//
//  Info.m
//  MOSAD_HW3
//
//  Created by enrique leo on 2020/12/14.
//

#import <Foundation/Foundation.h>
#import "Info.h"
#import "Masonry.h"
#import "AFNetworking.h"

@implementation Info

- (void)viewDidLoad{
    [super viewDidLoad];
    self.navigationItem.title = @"用户信息";
    self.view.backgroundColor = [UIColor colorWithRed:15/255.0 green:135/255.0 blue:219/255.0 alpha:1];
    
    UILabel *name = [[UILabel alloc]init];
    name.font = [UIFont systemFontOfSize: 20];
    name.text = @"Name: ";
    [self.view addSubview:name];
    UIEdgeInsets padding = UIEdgeInsetsMake(150, 5, 0, 0);
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(padding.top);
        make.left.equalTo(self.view.mas_left).with.offset(padding.left);
        make.height.mas_equalTo(40);
    }];
    
    UILabel *level = [[UILabel alloc]init];
    level.font = [UIFont systemFontOfSize: 20];
    level.text = @"Level: ";
    [self.view addSubview:level];
    padding = UIEdgeInsetsMake(30, 5, 0, 0);
    [level mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(name.mas_bottom).with.offset(padding.top);
        make.left.equalTo(self.view.mas_left).with.offset(padding.left);
        make.height.mas_equalTo(40);
    }];
    
    UILabel *email = [[UILabel alloc]init];
    email.font = [UIFont systemFontOfSize: 20];
    email.text = @"Email: ";
    [self.view addSubview:email];
    padding = UIEdgeInsetsMake(30, 5, 0, 0);
    [email mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(level.mas_bottom).with.offset(padding.top);
        make.left.equalTo(self.view.mas_left).with.offset(padding.left);
        make.height.mas_equalTo(40);
    }];
    
    UILabel *phone = [[UILabel alloc]init];
    phone.font = [UIFont systemFontOfSize: 20];
    phone.text = @"Phone: ";
    [self.view addSubview:phone];
    padding = UIEdgeInsetsMake(30, 5, 0, 0);
    [phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(email.mas_bottom).with.offset(padding.top);
        make.left.equalTo(self.view.mas_left).with.offset(padding.left);
        make.height.mas_equalTo(40);
    }];
    
    [self getInfo];
    
    self.name = [[UILabel alloc]init];
    self.name.font = [UIFont systemFontOfSize: 20];
    [self.view addSubview:self.name];
    padding = UIEdgeInsetsMake(150, 0, 0, 5);
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(name.mas_top).with.offset(0);
        make.right.equalTo(self.view.mas_right).with.offset(-padding.right);
        make.height.mas_equalTo(40);
    }];
    
    self.level = [[UILabel alloc]init];
    self.level.font = [UIFont systemFontOfSize: 20];
    [self.view addSubview:self.level];
    padding = UIEdgeInsetsMake(150, 0, 0, 5);
    [self.level mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(level.mas_top).with.offset(0);
        make.right.equalTo(self.view.mas_right).with.offset(-padding.right);
        make.height.mas_equalTo(40);
    }];
    
    self.email = [[UILabel alloc]init];
    self.email.font = [UIFont systemFontOfSize: 20];
    [self.view addSubview:self.email];
    padding = UIEdgeInsetsMake(150, 0, 0, 5);
    [self.email mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(email.mas_top).with.offset(0);
        make.right.equalTo(self.view.mas_right).with.offset(-padding.right);
        make.height.mas_equalTo(40);
    }];
    
    self.phone = [[UILabel alloc]init];
    self.phone.font = [UIFont systemFontOfSize: 20];
    [self.view addSubview:self.phone];
    padding = UIEdgeInsetsMake(150, 0, 0, 5);
    [self.phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phone.mas_top).with.offset(0);
        make.right.equalTo(self.view.mas_right).with.offset(-padding.right);
        make.height.mas_equalTo(40);
    }];
}

- (void) getInfo{
    NSString *URLString = [NSString stringWithFormat:@"http://172.18.176.202:3333/hw3/getinfo?name=%@", self.username];
    NSLog(@"url %@", URLString);
    //NSString *URLString = @"http://172.18.178.56/api/user/info/self";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:URLString parameters:nil headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
            NSLog(@"%@", responseObject);
            self.name.text = responseObject[@"name"];
            self.level.text = responseObject[@"level"];
            self.email.text = responseObject[@"email"];
            self.phone.text = responseObject[@"phone"];
            //NSLog(@"name %@", self.name);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"请求失败");
        }];
}

@end
